VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "GDIPlusPicture"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

Private Type GUID
   Data1    As Long
   Data2    As Integer
   Data3    As Integer
   Data4(7) As Byte
End Type

Private Type PICTDESC
   Size     As Long
   Type     As Long
   hBmp     As Long
   hPal     As Long
   Reserved As Long
End Type

Private Type GdiplusStartupInput
    GdiplusVersion           As Long
    DebugEventCallback       As Long
    SuppressBackgroundThread As Long
    SuppressExternalCodecs   As Long
End Type

Private Type PWMFRect16
    Left   As Integer
    Top    As Integer
    Right  As Integer
    Bottom As Integer
End Type

Private Type wmfPlaceableFileHeader
    Key         As Long
    hMf         As Integer
    BoundingBox As PWMFRect16
    Inch        As Integer
    Reserved    As Long
    CheckSum    As Integer
End Type

' GDI+ functions
Private Declare Function GdipLoadImageFromFile Lib "gdiplus.dll" (ByVal FileName As Long, GpImage As Long) As Long
Private Declare Function GdiplusStartup Lib "gdiplus.dll" (token As Long, gdipInput As GdiplusStartupInput, GdiplusStartupOutput As Long) As Long
Private Declare Function GdipCreateFromHDC Lib "gdiplus.dll" (ByVal hdc As Long, GpGraphics As Long) As Long
Private Declare Function GdipSetInterpolationMode Lib "gdiplus.dll" (ByVal Graphics As Long, ByVal InterMode As Long) As Long
Private Declare Function GdipDeleteGraphics Lib "gdiplus.dll" (ByVal Graphics As Long) As Long
Private Declare Function GdipDisposeImage Lib "gdiplus.dll" (ByVal Image As Long) As Long
Private Declare Function GdipGetImageWidth Lib "gdiplus.dll" (ByVal Image As Long, Width As Long) As Long
Private Declare Function GdipGetImageHeight Lib "gdiplus.dll" (ByVal Image As Long, Height As Long) As Long
Private Declare Function GdipDrawImageRectRectI Lib "gdiplus.dll" (ByVal Graphics As Long, ByVal GpImage As Long, ByVal dstx As Long, ByVal dsty As Long, ByVal dstwidth As Long, ByVal dstheight As Long, ByVal SrcX As Long, ByVal SrcY As Long, ByVal SrcWidth As Long, ByVal SrcHeight As Long, ByVal srcUnit As Long, ByVal imageAttributes As Long, ByVal callback As Long, ByVal callbackData As Long) As Long
Private Declare Sub GdiplusShutdown Lib "gdiplus.dll" (ByVal token As Long)

' GDI and GDI+ constants
Private Const InterpolationModeHighQualityBicubic = 7
Private Const GDIP_WMF_PLACEABLEKEY = &H9AC6CDD7
Private Const UnitPixel = 2


Private GdipToken       As Long
Private GdipInitialized As Boolean

Private Const GdiplusVersion     As Long = 1
Private Const CP_ACP            As Long = 0


Private Type ImageCodecInfo
   ClassID As GUID
   FormatID As GUID
   CodecName As Long
   DllName As Long
   FormatDescription As Long
   FilenameExtension As Long
   MimeType As Long
   Flags As Long
   Version As Long
   SigCount As Long
   SigSize As Long
   SigPattern As Long
   SigMask As Long
End Type


Private Enum SmoothingMode
    SmoothingModeInvalid = -1&
    SmoothingModeDefault = 0&
    SmoothingModeHighSpeed = 1&
    SmoothingModeHighQuality = 2&
    SmoothingModeNone = 3&
    SmoothingModeAntiAlias8x4 = 4&
    SmoothingModeAntiAlias = SmoothingModeAntiAlias8x4
End Enum

Private Enum GpStatus
    OK = 0
    GenericError = 1
    InvalidParameter = 2
    OutOfMemory = 3
    ObjectBusy = 4
    InsufficientBuffer = 5
    NotImplemented = 6
    Win32Error = 7
    WrongState = 8
    Aborted = 9
    FileNotFound = 10
    ValueOverflow = 11
    AccessDenied = 12
    UnknownImageFormat = 13
    FontFamilyNotFound = 14
    FontStyleNotFound = 15
    NotTrueTypeFont = 16
    UnsupportedGdiplusVersion = 17
    GdiplusNotInitialized = 18
    PropertyNotFound = 19
    PropertyNotSupported = 20
    ProfileNotFound = 21
End Enum

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

Private Declare Function lStrlenW Lib "kernel32" (ByVal lpString As Long) As Long
Private Declare Function WideCharToMultiByte Lib "kernel32" (ByVal codepage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Any, ByVal cchWideChar As Long, ByVal lpMultiByteStr As Any, ByVal cchMultiByte As Long, ByVal lpDefaultChar As String, ByVal lpUsedDefaultChar As Long) As Long


Private Declare Function GdipSaveImageToFile Lib "gdiplus" (ByVal Image As Long, ByVal FileName As Long, ByRef clsidEncoder As GUID, ByRef encoderParams As Any) As Long


Private Declare Function GdipGetImageEncodersSize Lib "gdiplus" (ByRef numEncoders As Long, ByRef Size As Long) As Long
Private Declare Function GdipGetImageEncoders Lib "gdiplus" (ByVal numEncoders As Long, ByVal Size As Long, ByRef Encoders As Any) As Long
Private Declare Function GdipSetSmoothingMode Lib "gdiplus" (ByVal Graphics As Long, ByVal SmoothingMode As SmoothingMode) As GpStatus

Private Declare Function GdipSaveGraphics Lib "gdiplus" (ByVal Graphics As Long, ByVal State As Long) As GpStatus

' Initialises GDI Plus
Private Function InitGDIPlus(ByVal token As Long) As Long
    Dim gdipInit As GdiplusStartupInput
    gdipInit.GdiplusVersion = 1
    GdiplusStartup token, gdipInit, ByVal 0&
    InitGDIPlus = token
End Function

' Frees GDI Plus
Private Sub FreeGDIPlus(token As Long)
    GdiplusShutdown token
End Sub

Public Sub GetImageSize(ByVal PicFile As String, ByVal ImgWidth As Long, ByVal ImageHeight As Long)
    Dim Img As Long, ImgW As Long, ImgH As Long
    GdipLoadImageFromFile StrPtr(PicFile), Img
    GdipGetImageWidth Img, ImgW
    GdipGetImageHeight Img, ImgH
    ImgWidth = ImgW
    ImageHeight = ImgH
    GdipDisposeImage Img
End Sub

'Private Function GetImageSize(ByVal ImageFile As String) As SizeL
'    Dim token As Long
'    Dim Img As Long
'    Dim gps As GdiplusStartupInput
'    gps.GdiplusVersion = 1
'
'    GdiplusStartup token, gps
'
'    GdipLoadImageFromFile StrPtr(ImageFile), Img
'    GdipGetImageWidth Img, GetImageSize.cx
'    GdipGetImageHeight Img, GetImageSize.cy
'    GdipDisposeImage Img
'
'    GdiplusShutdown token
'End Function

' Resize the picture using GDI plus
Public Function LoadPictureToHDC(strFile As String, hdc As Long, ByVal DestX As Long, ByVal DestY As Long, ByVal DestWidth As Long, ByVal DestHeight As Long, ByVal SrcX As Long, ByVal SrcY As Long, ByVal SrcWidth As Long, ByVal SrcHeight As Long) As Long
    On Error Resume Next
    Dim Graphics As Long
    Dim Img As Long
    Dim ImgH As Long, ImgW As Long
    
    GdipLoadImageFromFile StrPtr(strFile), Img
    
    GdipGetImageWidth Img, ImgW
    GdipGetImageHeight Img, ImgH
    
    GdipCreateFromHDC hdc, Graphics
    GdipSetInterpolationMode Graphics, InterpolationModeHighQualityBicubic
        
    LoadPictureToHDC = GdipDrawImageRectRectI(Graphics, Img, DestX, DestY, DestWidth * (DestWidth /, ImgHDC hdcd As Long) As Long
 * (e Fuj_fLong
 * (e Fuj_fLong
fLong
acd As Lo.dllPong
acd As Lo.dllTD As Lo.dllPong
aclga" (axterpols, InterEFuj_fLong
fLong
acd AscmnitGuj_fLong
fLeutdown token
End Suestown token
,(cd c Om

1yA Long) As Long
 * (e Fas Lo.dllPong
acd As Lo.dllTD As Lo.dllPong
aclce F As LongGeutdown tokSize Longs Lo.dll(fmFile StrPtr(strFile), Img
x,sibukeF As LongGeut&ng)

' GDI andeut&ng)

' GDI andeut&ng)
r Longs Lo.dll) ogdeutdown t,r)
r Longs Lo.dlleut&ng)W' GDI andeut&ng)
gs Lo.dll) ogdeUID, ByRef enongs Long, ByVal FaW enongs Long, B b
r Longs LciD, ByRefkGeuutdownkyAs Long
 * (eRvdownkyAs Long
Lo.dllPeage Img
En)=ong, B b
r Longn As Long
'u)=ong, B b
fmcv
r Longs LciD, ByRefkGeuutdownkyAs Long
 * (eRvdoon LoadPictureToHDC(strFilOGeuutdoeus Lo(mHDC(strFilOr,a(spo(strFile), Img
 yAs,N-riD, ByRefkGeuutdE, ByRefkGeuutVal  &ng)

' Gp Suestowa" TrFilOr,a(spo(strFile), Img
 yAs,N-riD&ng)W' GDI and,kSum    As I and As 0ge), Img
 yAsle),,ahpo(strFile), ImgtataEe), Img
 yAsle(tokeutd, Img
 yAsle(tokeutd, Img
 yAsGDIPlus = token
End Functionng, B b
r tr(ImageFile)(s String) As Sizt
End Funndeut&ngcSLt
End Funndeut&nng)keuaW ee)(s SEnd FunndeuaW ee)(s SEndeutuaW ee)(s Sau
EndeELt
Endwpw,S( b
deuaW ee)ID, ByRef entI anE ee)epand,kSu
Ende
 yAsle(tokeutd, ImgyVaeex-C entI anE ee)wnkyAs Long
 * tcatokebanE ee)wnkyAs tptee)kyAPO.ics As Long
L
 yAsle(tokeutd, Imgy,(rFilOr,a(spo(strFisle(tocyYNotIntI acp ee)eutuaWstWidth /, ImgH Vaeex-C entI anamee)krVxrAny,TkSum ConsAs I and As 0geeutuaWstW * t1ks I andw Gp Suestowa" I andwrGp Suestowa" I anE ee)wnkyAs tptee)kyAPO.ics As LoAs anE eetpteeanE ees AmFm
riD&ng)Wu"lOr,a(spo(strFisled,k
WrongS(mHDC(strFilOrFisleOeErVxrAny,ndeuaW ee)(s SEnd  Fo,6r
End Enum

Pd FunctstrFs LoAs anu*l DestWig)

Pd 1aue ConsndmFile StrP "kerney1aue ConsndmFmAPO.ics As LoAs anE eetpteeanE ey1a)

Pd eeanE ey1a)vDuney1aue xong,ney1aue Consndm(OrFisleeaW ilOr,a(spo(strFisyspo(strFissle cItrllTD = GdipDrawpo(strFissle cDSDndmFile StrP "tyUBrFissle cDSDndmFi6x( St.ficsFisedll" (ByVal Image As Long,ssle cDSDndmFi6x Lo.dllPong
aclgaxidth /,ction Private Declare FunctDvate Declare (e Fuj_fLopWdth /,ctFissleey Declare/,ction Private Declare Full" (ByVau(mHDC(stre/,ction Pria(ByVau(m*E ee)wntrPtr4tpuaWstW * t1kstrForGdipDrawpo(strcyVal Image As Long,ssle cDSDndutrcyskpgU"lOr,a(spo(staageFile As St1auunct1auua,(mHDC(strFisleeageFile As St1auunct6n.U,UID
   FormaID
ByVaFisleeageFi ,uua,(mHDC(strFisleeageFile As St1auunct6n.U,UID
 rcYhc FumFmAPO.ics As LoAs anE eeeFi ,uua,(mHDC(st('Fi ,uua,(mHDivaua,= ImgW
 HDC(strFisl
ByVaFisleeageFi ,ubleeageFi ,(wua,(mHDivaua,= ImgaN b,(wua,(mHDivaua,= ImgaN b,(wua,(mHDivaoAs anE eeegdipInitrDuua,(mHDC(strFis2mAosibukunction(y DetrWtrFis" I  geFi vaoAs anE eeegdipInitrDuua,(mHDC(strFis2mAs aiDestY As LoegdipInctioncng
 edutr'Acu"mHDC(song,ds LongR2pipIniire FunctDiire.dllPongIIIIIIIIIIIHDivaua,= Imga
CgaN b,(wua,(mHDyWImga
CgaN b,(wua(tokeuoo cID
MDInvaup tsInitrDuua,(mHTopH"vau.dllPonW2
MDInvR2pipIni
    GdipGetImageWidth Img, ImgW
    Gdiung
 edutr' LonedlImgW= Im
 rcYhc FumFmAPO.icsbstrcvmCAcImgW= Im
utr' Lops4IPcrcYhc Futn.U,UID
   FoUl De e
' GDIumFfics As tcsctnoI2
utr' Lops4IPcrHmops4IPcrHmopsn Img
 tokePtnoI2
utr'tr FunctionAePtnoI2r6g
 tokePics As tcsctnoI2
up
 tokePics Ase'tr Funcctn